<template>
	<view class="container" :class="skinStyle">
		<scroll-view class="top-tabs-view" scroll-x="true" :scroll-left="scrollLeft">
			<progress v-if="progressFlg" percent="100" activeColor="#e83f0a" active stroke-width="1" duration="5" class="progress" />
			<block v-for="(tab, index) in tabs" :key="index">
				<view class="tabs-item-wrap" v-bind:id="'tabNum' + index" @click="swichMenu(index)">
					<view class="tabs-item" :class="[currentTab == index ? 'tabs-item-act' : '']">
						<view class="tabs-item-txt">{{ tab.name }}</view>
						<view class="tabs-item-bottom"></view>
					</view>
				</view>
			</block>
		</scroll-view>

		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" :up="upOption" @up="upCallback" :fixed="true">
			<view class="tui-order-list">
				<block v-for="(item, index) in grouponInfo" :key="index">
					<!-- <tui-list-cell padding="0" class="b-bottom-none"> -->
					<view class="grouponList">
						<view class="tui-goods-item">
							<image :src="imgSrc + item.productThumbnailImageHdUrl" class="tui-goods-img"></image>
							<view class="tui-goods-center">
								<view class="tui-goods-name">{{ item.productName }}</view>
								<view class="tui-goods-attr">{{ item.memberLimit }}人团</view>
							</view>
							<view class="tui-price-right word-red">
								<view v-if="item.bargainStatus == 1">已成团</view>
								<view v-else-if="item.bargainStatus == 2">未成团</view>
								<view v-else>待成团</view>
							</view>
						</view>

						<!-- </tui-list-cell> -->
						<view class="b - bottom">
							<view class="tui-order-btn">
								<view class="tui-btn-ml" v-if="item.bargainStatus == 1||item.bargainStatus == 2">
									<tui-button type="primary" :plain="true" width="200rpx" height="62rpx" :size="28" shape="circle" @tap="result(item)">查看团详情</tui-button>
								</view>
								<view class="tui-btn-ml" v-else>
									<tui-button type="primary" :plain="true" width="200rpx" height="62rpx" :size="28" shape="circle" @tap="myGroupon(item.bargainId)">邀请好友拼团</tui-button>
								</view>
								<view class="tui-btn-ml">
									<tui-button type="black" :plain="true" width="200rpx" height="62rpx" :size="28" shape="circle" @tap="orderDetail(item.orderId)">
										查看订单详情
									</tui-button>
								</view>
							</view>
						</view>
					</view>
				</block>
			</view>
		</mescroll-body>
		<!-- <loading></loading> -->
		<tui-modal></tui-modal>
	</view>
</template>

<script>
import tuiButton from '@/components/button/button';
import tuiLoadmore from '@/components/loadmore/loadmore';
import tuiNomore from '@/components/nomore/nomore';
import tuiListCell from '@/components/list-cell/list-cell';
import mixin from '@/utils/mixin.js';
import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins.js';
export default {
	mixins: [mixin, MescrollMixin],
	components: {
		tuiButton,
		tuiLoadmore,
		tuiNomore,
		tuiListCell
	},
	data() {
		return {
			tabs: [
				{
					name: '全部'
				},
				{
					name: '待成团'
				},
				{
					name: '已成团'
				}
			],
			currentTab: 0,
			grouponInfo: [],
			imgSrc: this.$store.state.imgUrl,
			address: '',
			nickName: ''
		};
	},
	onLoad(options) {},
	filters: {},
	methods: {
		swichMenu: async function(current) {
			//点击其中一个 tab
			if (this.currentTab == current) {
				return false;
			} else {
				this.searchKey = '';
				this.progressFlg = true;
				this.currentTab = current;
				this.setScrollLeft(current);
				if (this.currentTab !== 6) {
					this.orderList = []; // 先置空列表,显示加载进度
				} else {
					this.postSaleList = [];
				}

				this.mescroll.resetUpScroll(); // 再刷新列表数据
			}
		},
		setScrollLeft: async function(tabIndex) {
			let leftWidthSum = 0;
			for (var i = 0; i <= tabIndex; i++) {
				let nowElement = await this.getWidth('tabNum' + i);
				if (nowElement) {
					leftWidthSum = leftWidthSum + nowElement.width;
				}
			}
			let winWidth = uni.getSystemInfoSync().windowWidth;
			this.scrollLeft = leftWidthSum > winWidth ? leftWidthSum - winWidth : 0;
		},
		getWidth: function(id) {
			//得到元素的宽高
			return new Promise((res, rej) => {
				uni.createSelectorQuery()
					.select('#' + id)
					.fields(
						{
							size: true,
							scrollOffset: true
						},
						data => {
							res(data);
						}
					)
					.exec();
			});
		},
		upCallback(page) {
			const params = {
				bargainStatus: this.getStatusParams(this.currentTab), //'':全部 0：待成团 1：已成团
				pageSize: page.size,
				current: page.num
			};

			this.$http
				.getGrouponList(params)
				.then(res => {
					if (res.status == 0) {
						this.progressFlg = false;
						let dataTemp = [];
						dataTemp = res.data.records;
						this.mescroll.endSuccess(dataTemp.length);
						if (page.num === 1) {
							
							// 如果是第一页需手动制空列表
							this.grouponInfo = [];
						}
						this.grouponInfo = this.grouponInfo.concat(dataTemp);
					}
				})
				.catch(() => {
					this.mescroll.endErr();
				});
		},

		getStatusParams(id) {
			switch (id) {
				case 0:
					return '';
				case 1:
					return '0';
				case 2:
					return '1';
			}
		},
		result(item) {
			var address = item.buyerInfo.areaLevel1 + item.buyerInfo.areaLevel2 + item.buyerInfo.areaLevel3 + item.buyerInfo.receiveAddress;
			uni.navigateTo({
				url: './grouponResult?bargainId=' + item.bargainId + '&address=' + address + '&name=' + item.buyerInfo.receiveName + '&time=' + item.bargainEndDate
			});
		},
		myGroupon(id) {
			uni.navigateTo({
				url: '../groupon/otherGroupon?bargainId=' + id
			});
		},
		orderDetail(id) {
			uni.navigateTo({
				url: '../../../packageA/pages/orderDetail/orderDetail?id=' + id
			});
		}
	}
};
</script>
<style lang="scss">
.grouponList {
	width: 97%;
	border-radius: 10rpx;
	margin: 10rpx auto;
}
.word-red {
	color: #e96d71;
}
.b-bottom {
	border-bottom: 1px solid #f2f2f2;
}
.imgMark {
	position: absolute;
	width: 100rpx !important;
	height: 100rpx !important;
	top: 0px;
	left: 12rpx;
	z-index: 100;
}
.progress {
	display: flex;
	top: 0;
	z-index: 9999999;
	width: 100%;
}
.container {
	padding-bottom: env(safe-area-inset-bottom);
	overflow: hidden;
	height: 100%;
	width: 100%;
}
/deep/ .uni-scroll-view::-webkit-scrollbar {
	/* 隐藏滚动条，但依旧具备可以滚动的功能 */
	display: none;
}
scroll-view [style*='overflow']::-webkit-scrollbar {
	display: none;
}
.top-tabs-view {
	width: 100%;
	box-sizing: border-box;
	display: flex;
	align-items: center;
	justify-content: space-between;
	z-index: 9999;
	box-shadow: 0 0 4rpx #e5e5e5;
	white-space: nowrap;
	height: 90rpx;
	position: fixed;
	left: 0;
	background: #fff;
	.tabs-item-wrap {
		display: inline-block;
		white-space: nowrap;
		height: 90rpx;
		width: 33.3%;
		.tabs-item {
			margin: 0 84rpx;
			position: relative;
			height: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			.tabs-item-txt {
				height: 90rpx;
				line-height: 90rpx;
				font-size: 28rpx;
				font-weight: 400;
				color: #333333;
			}
			.tabs-item-bottom {
				position: absolute;
				bottom: 0rpx;
				width: 130rpx;
				height: 4rpx;
				left: calc(50% - 65rpx);
			}
		}
		.tabs-item-act {
			.tabs-item-txt {
				//color:  $themeColor;
			}
			.tabs-item-bottom {
				//background:  $themeColor;
			}
		}
	}
}
.tui-order-list {
	margin-top: 80rpx;
	// margin-top: 192rpx;
}

.swiper-box-list {
	flex: 1;
	width: 100%;
	height: auto;
}
.swiper-one-list {
	height: 100%;
	width: 100%;
}

.swiper-one-list .swiper-list-entity {
	width: 100%;

	height: 112upx;

	text-align: center;

	display: flex;

	flex-wrap: wrap;

	align-items: center;

	justify-content: center;
}
.search-header-box {
	width: 100%;
	background: #fff;
	position: fixed;
	z-index: 99998;
	left: 0;
	top: 0;
	margin-top: 91rpx;
}
.search-header {
	display: flex;
	align-items: center;
	padding: 20rpx 20rpx;
	background: #fff;
	// margin-bottom: 20rpx;
}
.search-searchbox {
	flex: 1;
	height: 60rpx;
	border-radius: 30rpx;
	font-size: 24rpx;
	background: $borderColor;
	padding: 6rpx 20rpx;
	box-sizing: border-box;
	color: #999;
	display: flex;
	align-items: center;
	overflow: hidden;
}
.search-input {
	text-align: left !important;
	margin-left: 12rpx;
	width: 100% !important;
	background-color: $borderColor !important;
	.uni-input-input {
		width: 100%;
	}
}
.search-text {
	font-size: 32rpx;
	color: #444;
	margin: 0 20rpx 0 30rpx;
	color: $red;
}
.search-search-text {
	padding-left: 16rpx;
}
.search-search-key {
	max-width: 80%;
	height: 100%;
	padding: 0 16rpx;
	margin-left: 12rpx;
	display: flex;
	align-items: center;
	border-radius: 15px;
	background: rgba(0, 0, 0, 0.5);
	color: #fff;
}
.search-key-text {
	box-sizing: border-box;
	padding-right: 12rpx;
	font-size: 12px;
	line-height: 12px;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.tui-order-item {
	border-top: 16rpx solid #f5f5f5;
	overflow: hidden;
	position: relative;
	.red {
		color: $red;
	}
}
.tui-goods-title {
	width: 100%;
	font-size: 28rpx;
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.tui-order-status {
	color: #888;
	font-size: 26rpx;
}

.tui-goods-item {
	width: 100%;
	padding: 20rpx 30rpx;
	box-sizing: border-box;
	display: flex;
	justify-content: space-between;
	position: relative;
	background-color: #ffffff;
	border-bottom: 1rpx solid #f2f2f2;
}

.tui-goods-img {
	width: 180rpx;
	height: 180rpx;
	display: block;
	flex-shrink: 0;
	border-radius: 8rpx;
	image {
		border-radius: 8rpx;
	}
}

.tui-goods-center {
	flex: 1;
	padding: 20rpx 8rpx;
	box-sizing: border-box;
}

.tui-goods-name {
	max-width: 310rpx;
	word-break: break-all;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	font-size: 26rpx;
	line-height: 32rpx;
}

.tui-goods-attr {
	font-size: 22rpx;
	color: #888888;
	line-height: 32rpx;
	padding-top: 20rpx;
	word-break: break-all;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
}

.tui-price-right {
	text-align: right;
	font-size: 24rpx;
	color: #888888;
	line-height: 30rpx;
	padding-top: 20rpx;
}

.tui-color-red {
	color: #e41f19;
	padding-right: 30rpx;
}

.tui-goods-price {
	width: 100%;
	display: flex;
	align-items: flex-end;
	justify-content: flex-end;
	font-size: 24rpx;
}

.tui-size-24 {
	font-size: 24rpx;
	line-height: 24rpx;
}

.tui-price-large {
	font-size: 32rpx;
	line-height: 30rpx;
	font-weight: 500;
}

.tui-order-btn {
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: flex-end;
	background: #fff;
	padding: 10rpx 30rpx 20rpx;
	box-sizing: border-box;
}

.tui-btn-ml {
	margin-left: 20rpx;
}
.b-bottom-none {
	.tui-list-cell::after {
		border-bottom: none;
	}
}
</style>
